package org.atlantis.grosbeak.pot;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.atlantis.grosbeak.lang.Exceptions;

/**
 * 简单的、默认的数据源。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 */
public class InnerDataSource implements DataSource {

	private String driverClass;
	private String url;
	private String username;
	private String password;

	public InnerDataSource(String driverClass, String url, String username,
			String password) throws PotException {
		this.driverClass = driverClass;
		this.url = url;
		this.username = username;
		this.password = password;
		try {
			Class.forName(this.driverClass);
		} catch (ClassNotFoundException e) {
			throw Exceptions.makeThrow(PotException.class, e,
					"Unable to load class: '%s'", this.driverClass);
		}
	}

	public Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, username, password);
	}

	public Connection getConnection(String username, String password)
			throws SQLException {
		return getConnection();
	}

	private PrintWriter printWriter;
	private int timeout;

	public PrintWriter getLogWriter() throws SQLException {
		return printWriter;
	}

	public int getLoginTimeout() throws SQLException {
		return timeout;
	}

	public void setLogWriter(PrintWriter out) throws SQLException {
		this.printWriter = out;
	}

	public void setLoginTimeout(int seconds) throws SQLException {
		this.timeout = seconds;
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return false;
	}

	public <T> T unwrap(Class<T> iface) throws SQLException {
		return null;
	}
}
